<template>
  <div class="centers" v-loading="loading">
    <div class="topindexmeny">
      <IndexMenu />
    </div>
    <div class="center">
      <div class="centerforms">
        <el-form
          :model="ruleFormtext"
          :rules="rules"
          ref="ruleFormtext"
          class="demo-ruleForm"
        >
          <el-form-item label="人物视频" v-loading="isvideos" prop="videoUrl">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleVideoSuccess"
              :before-upload="beforeVideoUpload"
              :data="{ projectId: videowavid, pptSort: 1 }"
              name="resource"
              accept=".mp4"
            >
              <video
                v-if="ruleFormtext.videoUrl"
                :src="url + ruleFormtext.videoUrl"
                width="100%"
                height="100%"
                autoplay
                controls
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="语言文本" prop="text">
            <el-input
              type="textarea"
              placeholder="请输入文本内容"
              v-model="ruleFormtext.text"
              maxlength="300"
              show-word-limit
              style="width: 65%"
              :autosize="{ minRows: 4, maxRows: 8 }"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="voice"> </el-form-item>
          <el-form-item label="画面质量">
            <el-radio-group v-model="ruleFormtext.level">
              <el-radio :label="1">流畅</el-radio>
              <el-radio :label="2">蓝光</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('ruleFormtext')">合 成</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="centerhinforms">
        <div>音色</div>
        <div class="centerhinformss">
          <el-button
            v-for="item in list"
            :key="item.id"
            @click="show(item.name)"
          >
            {{ item.show_name }}
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import IndexMenu from "@/components/menu/IndexMenu.vue";
import { FastCombine } from "@/api/projectwavendmp3";
import { textToVideo, virtuatimbrellist } from "@/api/virtualhuman";
export default {
  name: "ProText",
  components: { IndexMenu },
  data() {
    return {
      loading: false,
      list: [],
      isvideos: false,
      url: this.$store.getters["url/urls"],
      videowavid: this.$route.query.videoid - 0,
      ruleFormtext: {
        videoUrl: "",
        text: "",
        voice: "",
        level: 1,
      },
      uploadUrl:
        this.$store.getters["url/urls"] + "virtual/v1/userPro/resUpload",
      rules: {
        videoUrl: [
          { required: true, message: "请选择人物视频", trigger: "change" },
        ],
        text: [{ required: true, message: "请输入文本", trigger: "change" }],
        voice: [
          { required: true, message: "请选择发声人物", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.isvideowav();
  },
  methods: {
    isvideowav() {
      if (!this.videowavid) {
        this.$message.error("非法跳转");
        this.$router.push("/");
      } else {
        this.virtuatimbrellists();
      }
    },
    handleVideoSuccess(res, file) {
      if (res.code === 200) {
        this.$message.success(res.msg);
        // this.ruleFormtext.videoUrl = URL.createObjectURL(file.raw);
        this.ruleFormtext.videoUrl = res.data.path;
      } else {
        this.$message.error(res.msg);
      }
      this.isvideos = false;
    },
    beforeVideoUpload(file) {
      const isJPG = file.type === "video/mp4";
      const isLt1G = file.size / 1024 / 1024 / 1024 < 1;
      this.isvideos = true;
      if (!isJPG) {
        this.$message.error("上传人物视频只能是 MP4 格式!");
        this.isvideos = false;
      }
      if (!isLt1G) {
        this.$message.error("上传人物视频大小不能超过 1G!");
      }
      return isJPG && isLt1G;
    },
    virtuatimbrellists() {
      virtuatimbrellist({ pageIndex: 1, pageSize: 50 }).then((res) => {
        if (res.code === 200) {
          this.list = res.data.list;
        } else {
          this.$message({
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    show(name) {
      this.ruleFormtext.voice = name;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          textToVideo({
            opt: 1,
            textList: [this.ruleFormtext.text],
            sampleRate: 1600,
            uuid: this.videowavid - 0,
            voice: this.ruleFormtext.voice,
          }).then((res) => {
            if (res.code === 200) {
              FastCombine({
                projectId: this.videowavid,
                text: this.ruleFormtext.text,
                videoFile: this.ruleFormtext.videoUrl,
                level: this.ruleFormwav.level,
                wavFile: res.data[0].Video,
              }).then((ress) => {
                if (ress.code === 200) {
                  this.$message.success(ress.msg);
                  this.loading = false;
                  this.$router.push("/");
                } else {
                  this.$message.error(ress.msg);
                  this.loading = false;
                }
              });
            } else {
              this.$message.error(res.msg);
              this.loading = false;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.centers {
  background-color: #f3f4f5;
  height: 100vh;
  box-sizing: border-box;
  .topindexmeny {
    position: sticky;
    top: 0;
    z-index: 999;
  }
  .center {
    height: 75vh;
    width: 90vw;
    margin: 2vh auto;
    background-color: #fff;
    padding: 4vh 5vw;
    box-sizing: border-box;
    .centerforms {
      float: left;
      width: 40%;
      height: 100%;
      .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
      }
      .avatar-uploader .el-upload:hover {
        border-color: #409eff;
      }
      .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 400px;
        height: 225px;
        line-height: 225px;
        text-align: center;
        border: 1px dashed #dcdfe6;
        background-color: #f5f7fa;
      }
      .avatar {
        width: 400px;
        height: 225px;
        display: block;
      }
      .wav {
        width: 400px;
        height: 100px;
        display: block;
      }
      .wav-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 400px;
        height: 100px;
        line-height: 100px;
        text-align: center;
        border: 1px dashed #dcdfe6;
        background-color: #f5f7fa;
      }
      .el-button {
        background-color: #434af9;
        color: #fff;
      }
    }
    .centerhinforms {
      float: left;
      text-align: left;
      background-color: #f5f7fa;
      padding: 30px;
      width: 40%;
      height: 55%;
      .centerhinformss {
        height: 70%;
        display: flex;
        flex-wrap: wrap;
        .el-button {
          height: 50px;
        }
      }
    }
  }
}
</style>